import 'dart:typed_data';

import 'package:dio/dio.dart' as dio;
import 'package:get/get.dart';
import 'package:largefriends/config/lf_route.dart';
import 'package:largefriends/entity/entities/lf_photo_entity.dart';
import 'package:largefriends/net/lf_api_url.dart';
import 'package:largefriends/net/lf_dio_util.dart';
import 'package:largefriends/widgets/dialog_action_tip.dart';
import 'package:largefriends/widgets/lf_image_picker.dart';
import 'package:wechat_camera_picker/wechat_camera_picker.dart';

import '../../../events/user_change_event.dart';
import '../../../service/manager/lf_event_helper.dart';
import '../../../net/error_handle.dart';
import '../../../net/lf_image_upload.dart';
import '../../../res/strings/lf_strings.dart';
import '../../../service/lf_auth_service.dart';
import '../../../utils/lf_dialog_utils.dart';

toVerifyIdConfirm({required AssetEntity image}) {
  Get.offAndToNamed(LFRoute.pathVerifyIDConfirm, arguments: {"image": image});
}

class LfVerifyIdConfirmLogic extends GetxController {
  late AssetEntity image;

  @override
  void onInit() {
    image = Get.arguments['image'];
    super.onInit();
  }

  verifyID() async {
    showLoading();
    var file = await image.file;
    var formData = dio.FormData();
    Uint8List? compressed = await compressPhoto(file!);
    if (compressed?.isNotEmpty ?? false) {
      formData.files.add(MapEntry(
          'file',
          dio.MultipartFile.fromBytes(compressed!,
              filename: DateTime.now().microsecondsSinceEpoch.toString())));
    } else {
      formData.files.add(MapEntry(
          'file',
          await dio.MultipartFile.fromFile(file!.path,
              filename: DateTime.now().microsecondsSinceEpoch.toString())));
    }
    formData.fields.add(const MapEntry('photoType', "3"));
    formData.fields.add(const MapEntry('protect', "1"));
    await LfDio()
        .post<List<LfPhotoEntity>>(
      url: apiUploadPhoto,
      params: formData,
      // onSendProgress: onSendProgress,
    )
        .then((value) {
      hideLoading();
      showActionTips(
          tips: strVerifyDone.tr,
          actionButtonTip: strOK.tr,
          action: () {
            Get.back();
            LfAuthService service = Get.find();
            service.currentAccount!.verified = 2;
            service.currentAccount!.mandatoryVerificationFlag = 0;
            service.setUserEntity(service.currentAccount);
            LfEventHelper().getEvent().fire(
                UserChangeEvent(userChangeAction: UserChangeAction.verify));
          },
          showCancel: false,
          dismissible: false);
    }, onError: (error) {
      hideLoading();
      if (error is NetError) {
        if (30002000 == error.code) {
          showActionTips(
              tips: strVerifyIDFailedTip.tr,
              actionButtonTip: strRetry.tr,
              action: () {
                LfImagePicker.pickerImage((file) {
                  if (file.isNotEmpty) {
                    image = file.first;
                    update();
                  }
                }, maxCount: 1);
              });
        } else {
          showSnackBar(error.msg);
        }
      }
    });
  }
}
